package edu.pku.hawk.controller.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Util {

	public static void logln(Object log) {
		if (Constants.PrintLog) {
			System.out.println(log.toString());
		}
	}
	
	public static void log(Object log) {
		 if (Constants.PrintLog) {
			 System.out.print(log.toString());
		 }
	}

	public static boolean isBlankOrNull(Object value) {
		if (value instanceof String) {
			return ((value == null) || (value.toString().trim().length() == 0));
		} else if (value instanceof Integer) {
			return ((value == null) || ((Integer) value == 0));
		} else if (value != null) {
			Class valueClass = value.getClass();
			if (valueClass.isArray()) {
				return Array.getLength(value) == 0;
			}
			if (List.class.isAssignableFrom(valueClass)) {
				System.out.println(((List) value).size());
				return ((List) value).size() == 0;
			}
			return false;
		} else {
			return value == null;
		}
	}

	public static String trim(String str) {
		return (str == null) ? "" : str.trim();
	}
	
	public static String readFile(String filePath) {
		StringBuffer sb = new StringBuffer();
		ArrayList<String> lines = readFileLines(filePath);
		for (String line : lines) {
			sb.append(line);
		}
		return sb.toString();
	}

	public static ArrayList<String> readFileLines(String filePath) {
		FileInputStream fr = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		ArrayList<String> lines = new ArrayList<String>();
		InputStream is = null;
		try {
			is = Util.class.getClassLoader().getResourceAsStream(filePath);
			if (is == null) {
				File file = new File(filePath);
				if (!file.exists()) {
					return null;
				}
				fr = new FileInputStream(file);
				isr = new InputStreamReader (fr, "utf-8");
				br = new BufferedReader(isr);
			} else {
				isr = new InputStreamReader(is, "utf-8");
				br = new BufferedReader(isr);
			}

			String str = null;
			while ((str = br.readLine()) != null) {
				lines.add(str);
			}
			return lines;
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (fr != null) {
					fr.close();
				}
				if (br != null) {
					br.close();
				}
				if (isr != null) {
					isr.close();
				}
				if (is != null) {
					is.close();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return lines;
	}

	public static Properties loadPropertyFile(String filePath) {
		Properties ret = null;
		InputStream is = null;
		try {
			is = Util.class.getClassLoader().getResourceAsStream(filePath);
			if (is == null) {
				File file = new File(filePath);
				if (!file.exists())
					return null;
				is = new FileInputStream(file);
			} 
			ret = new Properties();
			ret.load(is);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
		return ret;
	}
	
	public static String getLocalIPAddress()
	{
		InetAddress address;
		String ip=null;
		try {
			address = InetAddress.getLocalHost();
			ip=address.getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return ip;
	}

	public static String generateRMIAddress(String ipAddress,String port,String serviceName){
		return "//"+ipAddress+":"+port+"/SensorDataService";
	}
	
	public static void main(String[] args) {
		System.out.println(getLocalIPAddress());
		System.out.println(generateRMIAddress(getLocalIPAddress(),"9000","SensorDataService"));
	}
}
